<?php
/**
 * MEEPO 米波现场
 *
 * 官网 http://meepo.com.cn 作者QQ 284099857
 */
global $_GPC, $_W;
load()->func("logging");
$weid = $_W['uniacid'];
if($_W['isajax']){
	$rid = intval($_GPC['rid']);
	$award_id = intval($_GPC['award_id']);
	$users = $_GPC['users'];
    logging_run("中奖用户=>".json_encode($users),"info");
	$lottory_config = pdo_fetch("SELECT `send_mess`,`def_mess` FROM ".tablename($this->lottory_config_table)." WHERE weid=:weid AND rid=:rid",array(":weid"=>$weid,":rid"=>$rid));
	$award = pdo_fetch("SELECT `tag_name`,`luck_name` FROM ".tablename($this->lottory_award_table)." WHERE weid=:weid AND rid=:rid AND id=:id",array(':weid'=>$weid,':rid'=>$rid,':id'=>$award_id));
	$data = array();
	foreach($users as $temp) {
		$user_id = intval($temp['id']);
		$openid = $temp['openid'];
		$sql = "SELECT `nick_name`,`avatar` FROM ".tablename($this->user_table)." WHERE openid = :openid AND rid = :rid  AND weid=:weid";
		$param = array(':openid' => $openid, ':rid' => $rid,':weid' => $weid);
		$temp_user = pdo_fetch($sql,$param);
		$insert = array();
		$insert['weid'] = $weid;
		$insert['rid'] = $rid;
		$insert['openid'] = $openid;
		$insert['nick_name'] = $temp_user['nick_name'];
		$insert['avatar'] = $temp_user['avatar'];
		$insert['user_id'] = $user_id;
		$insert['lottory_id'] = $award_id;
		$insert['createtime'] = time();
		
		pdo_insert($this->lottory_user_table ,$insert);
		$content = "亲爱的".$temp_user['nick_name']."\n恭喜恭喜！\n你已经中: ".$award['tag_name']."\n奖品为: ".$award['luck_name']."\n请按照主持人的提示，到指定地点领取您的奖品！\n您的获奖验证码是: ".time();
		$this->send_message($rid,$openid,$content);
		$insert_id = pdo_insertid();
		if(!$insert_id){
			$insert_id = pdo_fetchcolumn("SELECT `id` FROM ".tablename($this->lottory_user_table)." WHERE weid=:weid AND rid=:rid AND lottory_id=:lottory_id ORDER BY id DESC LIMIT 1",array(':weid'=>$weid,':rid'=>$rid,':lottory_id'=>$award_id));
		}
		$data[] = $insert_id;
		unset($temp);
		unset($temp_user);
		unset($content);
	}
	
	$result = array('result'=>0,'data'=>$data);
	die(json_encode($result));
}